import Allregular from "../../../utils/regular"
import http from "../../../utils/http"
const common = require('../../../public.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        realName:"",//名字验证
        cardNo:"",//身份证验证
        isRealnames:"",
        userInfo:{}, //用户信息
        ids:'',
        str:""
    },
//身份证失焦
identityBlur(){
        let result = Allregular.identity(this.data.cardNo)
        if(!result){
            wx.showToast({
              title: '请核对您的身份证信息',
              icon:"none"
            })
            return
        }
    },
        //点击认证
   async toauthentication(){
    let {realName,cardNo} = this.data
    let option = {
        url:"/user/verify",
        data:{
            userId:wx.getStorageSync('user').user.id,
            token:wx.getStorageSync('user').token,
            realName,
            cardNo
        }
    }
    let res = await http(option)
    console.log(res);
    if(res.data.status == 200){
        wx.showToast({
          title: "验证成功",
        })
        let user = wx.getStorageSync('user')
        user.user.isRealname = 1
        wx.setStorageSync('user', user)
    }else{
        wx.showToast({
            title: res.data.msg,
            icon:"none"
          })
    }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        common.getLocalUserInfo(function (success, data) {
            if (success) {
            
               let certificateNumber=data.user.ids;
            //    certificateNumber = certificateNumber.replace(/(?<=\d{3})\d{12}(?=\d{2})/,"************")
               var str = data.user.realName
              
               if(null != str && str != undefined){
                if(str.length==2){
                    str= str.substring(0,1)+'*' //截取name 字符串截取第一个字符，
                }else if(str.length==3){
                    str=str.substring(0,1)+"*"+str.substring(2,3)//截取第一个和第三个字符
                }else if(str.length>3){
                    str= str.substring(0,1)+"*"+'*'+str.substring(3,str.length)//截取第一个和大于第4个字符
                }
            }
             else {
                return "";
            }
           
               that.setData({
                   isRealnames:data.user.isRealname,  //1已实名 2未实名
                   userInfo:data.user,
                   ids:certificateNumber,
                   str
               })
            }else{
                wx.navigateTo({
                  url: '/pages/login/index',
                })
            }
        })
    },
})